@extends('admin::public.base')
@section('content')
    <div class="layui-card">
        <div class="layui-header">
            <div class="layui-card-header layuiadmin-card-header-auto">
                @if (auth('admin')->user()->can('asdfasdnf'))
                    <button class="layui-btn" url="{{ route('admin.permission.create') }}" permissionId="{{isset($permissionId) ? $permissionId : 0 }}" id="itemAdd">添 加</button>
                @endif
                <button class="layui-btn layui-btn-primary" id="returnParent" pid="{{isset($pid) ? $pid : 0}}">返回上级</button>
            </div>
        </div>
        <table id="dataTable" class="layui-hide" lay-filter="dataTable"></table>
        <script type="text/html" id="options">
            @if(auth('admin')->user()->can('system.permission.lower'))
            <a class="normal" lay-event="children">下级</a>
            @endif
            <a class="normal" lay-event="edit">编辑</a>
            <a class="normal" lay-event="del">删除</a>
        </script>
    </div>
@endsection

@section('script')
        <script>
            layui.use(['layer','table','form'],function () {
                var layer = layui.layer;
                var form = layui.form;
                var table = layui.table;
                var $ = layui.$;
                var param = {!!$param!!};
                var page = param.page?param.page:1;
                var parent_id = param.parent_id?param.parent_id:$("#itemAdd").attr('permissionId');
                var limit = param.limit?param.limit:10;
                //用户表格初始化
                var dataTable = table.render({
                    elem: '#dataTable'
                    ,skin:'line'
                    ,cellMinWidth: 150
                    ,url: "{{ route('admin.permission.getList') }}" //数据接口
                    ,where:{'parent_id':parent_id}
                    ,text: { //自定义文本，此处用法--》当返回数据为空时的异常提示
                        none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                    }
                    ,page: {
                        curr:page,
                        limit:limit
                    }
                    ,cols: [[ //表头
                        {checkbox: true}
                        ,{field: 'name', title: '模块配置', align:'center'}
                        ,{field: 'display_name', title: '模块名称', align:'center'}
                        ,{field: 'route', title: '路由', align:'center'}
                        ,{field: 'icon', title: '图标',align:'center',templet: function(row) {
                            return '<i class="fa '+row.icon+'"></i>';
		                }}
                        ,{field: 'created_at',align:'center', title: '创建时间'}
                        ,{field: 'updated_at',align:'center', title: '更新时间'}
                        ,{title:'操作',align:'center', toolbar: '#options'}
                    ]]
                });
                //监听复选框
                table.on('checkbox(dataTable)', function(obj){
                    if(obj.checked){
                        if(obj.type=="all"){
                            $('.layui-table').find('tbody tr').attr({"style":"background:#f5fbff"});
                        }else{
                            var index = obj.data.LAY_TABLE_INDEX;
                            $('.layui-table').find('tbody tr').eq(index).attr({"style":"background:#f5fbff"});
                        }
                    }else{
                        if(obj.type=="all"){
                            $('.layui-table').find('tbody tr').attr({"style":"background:#ffffff"});
                        }else{
                            var index = obj.data.LAY_TABLE_INDEX;
                            $('.layui-table').find('tbody tr').eq(index).attr({"style":"background:#ffffff"});
                        }
                    }
                });
                //监听工具条
                table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                    var data = obj.data //获得当前行数据
                        ,layEvent = obj.event; //获得 lay-event 对应的值
                    if(layEvent === 'del'){
                        layer.confirm('您确认要删除吗？',{ icon: 3, title: '温馨提示', skin: 'layui-skin'}, function(index){
                            $.post("{{ route('admin.permission.destroy') }}",{_method:'delete',ids:[data.id]},function (data) {
                                if (data.code == 1) {
                                    layer.msg(data.msg, {
                                        time: 2000
                                    }, function () {
                                        reload(parent_id);
                                    })
                                } else {
                                    layer.msg(data.msg);
                                }
                                layer.close(index);
                            });
                        });
                    } else if(layEvent === 'edit'){
                        location.href = '/admin/permission/'+data.id+'/edit';
                    } else if (layEvent === 'children'){
                        var pid = $("#returnParent").attr("pid");
                        if (data.parent_id!=0){
                            $("#returnParent").attr("pid",pid+'_'+data.parent_id);
                        }
                        $("#itemAdd").attr('permissionId', data.id);
                        reload(data.id);
                    }
                });

                //返回上一级
                $("#returnParent").click(function () {
                    var pid = $(this).attr("pid");
                    if (pid!='0'){
                        ids = pid.split('_');
                        parent_id = ids.pop();
                        $(this).attr("pid",ids.join('_'));
                    }else {
                        parent_id=pid;
                    }
                    reload(parent_id);
                })

                function reload(parent_id){
                    dataTable.reload({
                        where:{parent_id:parent_id},
                        page:{curr:1}
                    })
                }
                $("#itemAdd").click(function() {
                    var url = $(this).attr('url');
                    var id = $(this).attr('permissionId');
                    window.location.href = url+'?id='+id;
                })
            })
        </script>
@endsection
